package com.example.medical.controller.admin;


import com.example.medical.entity.dto.UserGetNoticeListDto;
import com.example.medical.service.NoticeService;
import com.example.medical.utils.common.enums.Code;
import com.example.medical.utils.common.result.Result;
import com.example.medical.utils.config.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/admin/notice")
public class AdminNoticeController {

    @Autowired
    private NoticeService noticeService;

    @PostMapping("/get_notice_list")
    public Result AdminGetNoticeList(@RequestBody UserGetNoticeListDto dto){
        return noticeService.UserGetNoticeList(dto);
    }

    @GetMapping("/get_notice_detail")
    public Result AdminGetNoticeDetail(@RequestParam("id") Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        return Result.okResult(noticeService.getById(id));
    }

    @GetMapping("/remove_notice")
    public Result AdminRemoveNotice(@RequestParam("id")Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        boolean checkRemove = noticeService.removeById(id);
        if (!checkRemove) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        return Result.okResult(Code.SUCCESS);
    }

    @GetMapping("/remove_notice_arr")
    public Result AdminRemoveNoticeArr(@RequestParam("ids")String[] ids){
        return noticeService.AdminRemoveNoticeArr(ids);
    }

    @GetMapping("/update_get_notice")
    public Result AdminUpdateGetNotice(@RequestParam("id")Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        return Result.okResult(noticeService.getById(id));
    }

    @PostMapping("/update_notice")
    public Result AdminUpdateNotice(@RequestParam("id")Long id,
                                    @RequestParam(value = "name",required = false)String name,
                                    @RequestParam(value = "typeId",required = false)Long typeId,
                                    @RequestParam(value = "file",required = false)MultipartFile file,
                                    @RequestParam(value = "message",required = false)String message) throws Exception {
        return noticeService.AdminUpdateNotice(id,name,typeId,file,message);
    }

    @PostMapping("/save_notice")
    public Result AdminSaveNotice(@RequestParam(value = "name",required = false)String name,
                                  @RequestParam(value = "typeId",required = false)Long typeId,
                                  @RequestParam(value = "file",required = false)MultipartFile file,
                                  @RequestParam(value = "message",required = false)String message) throws Exception {
        return noticeService.AdminSaveNotice(name,typeId,file,message);
    }

}
